﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
namespace ITXHWEB.Controllers
{
    public partial class VVTLIEFController : ITXController
    {
        /*
         <script type="text/javascript">
    var txtvkp = document.getElementById("vkp");
    txtvkp.focus();
    if (txtvkp.textContent.toString().length > 7)
    {
        var txtlief = document.getElementById("lief");
        txtlief.focus();
    }   
</script>
         */


        public VVTLIEFController()
        {
            MESSAGES += "VVTLOAD;";

        }

        /// <summary>
        /// 展示当前记录
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult MVVTLIEF(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.VVTS = VVTS;
            ViewBag.MVVT = MVVT;
            ViewBag.ReturnUrl = returnUrl;
            VVTLIEFModel mx = new VVTLIEFModel(); 
            mx.REMARK = (TWBS - HWBS).ToString() + "-待入库:" + CPWBS.ToString();
            mx.FARBE = HWBS.ToString() + "-已入库";
            mx.ART = VKPAUF + LIEFSCH;
            return PartialView(mx);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult MVVTLIEF(VVTLIEFModel model, string returnUrl)
        {
            IsVKPReady = false;
            IsVVTReady = false;
            VKPAUF = "";
            LIEFSCH = "";
            TWBS = 0;
            HWBS = 0;
            try
            {
                VHP.WBW.POSTCHG();
            }
            catch (Exception ex)
            {
                VKPAUF = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
            //return RedirectToAction("ITXWB06", "ITXWB06", new { TempData = TempData });
            //return View(model);
        }
        [ChildActionOnly]
        public ActionResult SVVTLIEF(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            VVTLIEFModel m = new VVTLIEFModel();
            m.TERMNO = app.TERMNO;
            m.VKPAUF = VKPAUF;
            m.LIEFSCH = LIEFSCH;
            if (!IsVVTReady)
            {
                m.REMARK = VKPAUF;
            }
            return PartialView(m);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SVVTLIEF(VVTLIEFModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                LIEFSCH = model.LIEFSCH.Trim();
                VVTLOAD(model);
            }
            catch (Exception ex)
            {
                VKPAUF = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
            //return RedirectToAction("ITXWB06", "ITXWB06", new { TempData = TempData });
            //return View(model);
        }

        [ChildActionOnly]
        public ActionResult SVVTVKP(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            VVTLIEFModel m = new VVTLIEFModel();
            m.TERMNO = app.TERMNO;
            m.VKPAUF = VKPAUF;
            if (!IsVKPReady)
            {
                m.REMARK = "请扫描有效的单号";
            }
            return PartialView(m);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SVVTVKP(VVTLIEFModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                VKPAUF = model.VKPAUF.Trim();
                VVTLOAD(model);
            }
            catch (Exception ex)
            {
                VKPAUF = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
            //return RedirectToAction("ITXWB06", "ITXWB06", new { TempData = TempData });
            //return View(model);
        }
        public void VVTLOAD(VVTLIEFModel model)
        {
            WBBS = null;
            IsVVTReady = false;
            if (VKPAUF == null)
                return;
            if (VKPAUF.Trim().Length < 8)
            {
                IsVKPReady = false;

            }
            else
            {
                IsVKPReady = true;
            }
            if (LIEFSCH == null)
                return;
            if (LIEFSCH.Trim().Length < 5)
            {
                IsVVTReady = false;
                return;

            }
            VKPAUF = VKPAUF.Trim().PadRight(11);
            PM["AUFTYP"] = "K";
            PM["KRS"] = VKPAUF.Substring(0, 2);
            PM["LFD"] = VKPAUF.Substring(2, 6);
            PM["POS"] = VKPAUF.Substring(8, 3);
            PM["LIEFSCH"] = LIEFSCH.Trim().PadRight(8);

            DataSet SResult = VHP.VVT.VVTLOAD(PM);
            if (VVTS == null)
            {
                VVTS = new List<VVTLIEFModel>();
            }
            VVTS.Clear();
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                VVTLIEFModel m = new VVTLIEFModel();
                m.LIEFSCH = row["VVTLIEFSCH"].ToString();
                m.AUFTYP = row["VVTAUFTYP"].ToString();
                m.KRS = row["VVTAUFKRS"].ToString();
                m.LFD = row["VVTAUFLFD"].ToString();
                m.POS = row["VVTAUFPOS"].ToString();
                m.VKPAUF = row["VVTAUFKRS"].ToString() + row["VVTAUFLFD"].ToString() + row["VVTAUFPOS"].ToString();
                m.VFART = row["VVTART"].ToString() + row["VVTFARBE"].ToString();
                m.WAKZ = row["VVTWAKZ"].ToString();
                m.ART = row["VVTART"].ToString();
                m.VAR = row["VVTVAR"].ToString();
                m.FARBE = row["VVTFARBE"].ToString();
                VVTS.Add(m);
                MVVT = m;
                IsVVTReady = true;
            }
        }
    }
}
